'use strict'

module.exports = (app) => {
	const { STRING, INTEGER, DATE, BOOLEAN } = app.Sequelize

	const User = app.model.define(
		'users',
		{
			id: {
				type: INTEGER,
				primaryKey: true,
				autoIncrement: true,
				allowNull: true,
			},
			username: STRING(30),
			password: INTEGER,
			avatar: STRING,
			phone: INTEGER,
			status: {
				type: BOOLEAN,
				defaultValue: true,
			},
			createdAt: DATE,
			updatedAt: DATE,
			deletedAt: DATE,
		},
		{
			tableName: 'users', //指定表名称
			timestamps: true,
		}
	)
	// User.associate = function () {
	// 	User.belongsTo(app.model.Role, {
	// 		foreignKey: 'role_id',
	// 	})
	// }
	User.associate = function () {
		User.belongsToMany(app.model.Role, {
			through: app.model.RoleUser,
			foreignKey: 'userId',
			otherKey: 'roleId',
		})
	}

	return User
}
